/**
 * 求数组前n项和
 *
 * @Author zhengyuefan
 * @Date 2024/10/3
 */

public class Sum {
    public static int sum(int arr[]) {
        return sum(arr, 0);
    }

    /**
     * 递推求数组前n项和
     *
     * @param arr
     * @param left 数组左边界
     * @return
     */
    private static int sum(int arr[], int left) {
        if (left == arr.length) {
            return 0;
        }
        return arr[left] + sum(arr, left + 1);
    }

    public static void main(String[] args) {
        int arr[] = {1, 2, 3, 4};
        System.out.println(sum(arr));
    }
}
